#pragma once
#include "uart/ProtocolSender.h"
#include "utils/TimeHelper.h"
#include "storage/StoragePreferences.h"
#include "utils/TimeHelper.h"
#include <ntp/ntp.h>






static int year;
static int mon;
static int day;
static int hour;
static int minn;


const int dayTab[]={31,28,31,30,31,30,31,31,30,31,30,31,30};
const int daycTab[]={31,29,31,30,31,30,31,31,30,31,30,31,30};
static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
	//{0,  6000}, //定时器id=0, 时间间隔6秒
	{1,  2000},
};

void updateTimeUi()
{
	char timeYear[64];
	char timeMon[64];
	char timeDay[64];
	char timeHour[64];
	char timeMin[64];
	struct tm *t = TimeHelper::getDateTime();
	t->tm_year==70 ?
	(sprintf(timeYear,"%02d",1900 + t->tm_year+52),year=1900 + t->tm_year+52)
	: (sprintf(timeYear,"%02d",1900 + t->tm_year),year=1900 + t->tm_year);
	sprintf(timeMon,"%02d",t->tm_mon+1);
	sprintf(timeDay,"%02d",t->tm_mday);
	sprintf(timeHour,"%02d",t->tm_hour);
	sprintf(timeMin,"%02d",t->tm_min);
	mon=t->tm_mon+1;
	day=t->tm_mday;
	hour=t->tm_hour;
	minn=t->tm_min;
	mTextViewYearPtr->setText(timeYear);
	mTextViewMonPtr->setText(timeMon);
	mTextViewMonnPtr->setText(timeDay);
	mTextViewHourPtr->setText(timeHour);
	mTextViewMinPtr->setText(timeMin);

}
int synchronization_time()
{
	//先设置本地时区
	setenv("TZ", "UTC-8", 1); //这里UTC-8表示北京时区
	tzset();

	//启动同步时间任务
	return ntp::startSynchronizationTask(ntp::defaultServerList(), NULL);
}
static void TimeSet(){
	synchronization_time();
	mnetTimeWndPtr->hideWnd();
	mButtonSavePtr->setInvalid(true);
	mhidePtr->setInvalid(true);
}


/**
 * 当界面构造时触发
 */
static void onUI_init(){
	mButtonAutoTimePtr->setSelected(StoragePreferences::getInt("AutoTime", 1));
	if(mButtonAutoTimePtr->isSelected()){
		TimeSet();
	}
	updateTimeUi();

}

/**
 * 当切换到该界面时触发
 */
static void onUI_intent(const Intent *intentPtr) {
    if (intentPtr != NULL) {
        //TODO
    }
}

/*
 * 当界面显示时触发
 */
static void onUI_show() {

}

/*
 * 当界面隐藏时触发
 */
static void onUI_hide() {

}

/*
 * 当界面完全退出时触发
 */
static void onUI_quit() {

}

/**
 * 串口数据回调接口
 */
static void onProtocolDataUpdate(const SProtocolData &data) {

}

/**
 * 定时器触发函数
 * 不建议在此函数中写耗时操作，否则将影响UI刷新
 * 参数： id
 *         当前所触发定时器的id，与注册时的id相同
 * 返回值: true
 *             继续运行当前定时器
 *         false
 *             停止运行当前定时器
 */
static bool onUI_Timer(int id){
	switch (id) {
	case 1:
		//开启自动同步才每秒刷新时间
		if(mButtonAutoTimePtr->isSelected())
		{
			updateTimeUi();
		}
		break;
		default:
			break;
	}
    return true;
}

/**
 * 有新的触摸事件时触发
 * 参数：ev
 *         新的触摸事件
 * 返回值：true
 *            表示该触摸事件在此被拦截，系统不再将此触摸事件传递到控件上
 *         false
 *            触摸事件将继续传递到控件上
 */
static bool ontimeActivityTouchEvent(const MotionEvent &ev) {
    switch (ev.mActionStatus) {
		case MotionEvent::E_ACTION_DOWN://触摸按下
			//LOGD("时刻 = %ld 坐标  x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
			break;
		case MotionEvent::E_ACTION_MOVE://触摸滑动
			break;
		case MotionEvent::E_ACTION_UP:  //触摸抬起
			break;
		default:
			break;
	}
	return false;
}


static bool onButtonClick_ButtonAutoTime(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonAutoTime !!!\n");

    pButton->setSelected(!pButton->isSelected());
    StoragePreferences::putInt("AutoTime", pButton->isSelected());
    if(pButton->isSelected()){
    	TimeSet();

    	LOGD("自动网络同步时间\n");
    }else{
    	mnetTimeWndPtr->showWnd();
    	mButtonSavePtr->setInvalid(false);
    	mhidePtr->setInvalid(false);
    }
    updateTimeUi();
    return false;
}
static bool onButtonClick_ButtonSave(ZKButton *pButton) {
	struct tm t;
	t.tm_year = year-1900;
	t.tm_mon = mon - 1;
	t.tm_mday = day;
	t.tm_hour = hour;
	t.tm_min = minn;
    t.tm_sec = 0;
	TimeHelper::setDateTime(&t);
	EASYUICONTEXT->goBack();
    return false;
}

void modsetdat(int days)
{
 char timeDay[64];
// LOGD("days=%d,day=%d\n",days,day);
    if(day >= days)
    {
    	day=days;
        sprintf(timeDay,"%02d",days);
        mTextViewMonnPtr->setText(timeDay);
    	return ;
    }
    sprintf(timeDay,"%02d",day);
    mTextViewMonnPtr->setText(timeDay);
}

static bool onButtonClick_ButtonYearAdd(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonYearAdd !!!\n");
    char timeYear[64];
    if(year >= 2038)
    {
    	return false;
    }
    else
    {
    	year++;
    }
    sprintf(timeYear,"%02d",year);
    mTextViewYearPtr->setText(timeYear);

    if((year%4 == 0 && year%100 !=0) || (year%400 == 0))
    {
    	modsetdat(daycTab[mon-1]);
    }
    else
    {
    	modsetdat(dayTab[mon-1]);
    }
    return false;
}

static bool onButtonClick_ButtonYearSub(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonYearSub !!!\n");
    char timeYear[64];
    if(year <2021)
    {
    	return false;
    }
    else
    {
    	year--;
    }
    sprintf(timeYear,"%02d",year);
    mTextViewYearPtr->setText(timeYear);


    if((year%4 == 0 && year%100 !=0) || (year%400 == 0))
    {
    	modsetdat(daycTab[mon-1]);
    }
    else
    {
    	modsetdat(dayTab[mon-1]);
    }
    return false;
}
void setmon()
{
    char timeMon[64];
    if(mon >= 12)
    {
    	mon = 1;
    }
    else
    {
    	mon++;
    }
    sprintf(timeMon,"%02d",mon);
    mTextViewMonPtr->setText(timeMon);
}

void setdat(int days)
{
 char timeDay[64];
    if(day >= days)
    {
    	day = 1;
    }
    else
    {
    	day++;
    }
    sprintf(timeDay,"%02d",day);
    mTextViewMonnPtr->setText(timeDay);
}


static bool onButtonClick_ButtonMonAdd(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonMonAdd !!!\n");
    setmon();
    if((year%4 == 0 && year%100 !=0) || (year%400 == 0))
    {
    	LOGD("闰年 29 \n");
    	modsetdat(daycTab[mon-1]);
	   LOGD("daycTab[mon-1]=%d\n",daycTab[mon-1]);
    }
    else
    {
    	LOGD("平年  28 \n");
    	modsetdat(dayTab[mon-1]);
    	LOGD("dayTab[mon-1]=%d\n",dayTab[mon-1]);
    }
    return false;
}

static bool onButtonClick_ButtonDayAdd(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonDayAdd !!!\n");

    if((year%4 == 0 && year%100 !=0) || (year%400 == 0))
    {
    	LOGD("闰年 29 \n");
	   setdat(daycTab[mon-1]);
	   LOGD("daycTab[mon-1]=%d\n",daycTab[mon-1]);
    }
    else
    {
    	LOGD("平年  28 \n");
    	setdat(dayTab[mon-1]);
    	LOGD("dayTab[mon-1]=%d\n",dayTab[mon-1]);
    }

    return false;
}

static bool onButtonClick_ButtonHourAdd(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonHourAdd !!!\n");
    char timeHour[64];
    if(hour >= 23)
    {
    	hour = 0;
    }
    else
    {
    	hour++;
    }
    sprintf(timeHour,"%02d",hour);
    mTextViewHourPtr->setText(timeHour);
    return false;
}

static bool onButtonClick_ButtonMinAdd(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonMinAdd !!!\n");
    char timeMin[64];
    if(minn >= 59)
    {
    	minn = 0;
    }
    else
    {
    	minn++;
    }
    sprintf(timeMin,"%02d",minn);
    mTextViewMinPtr->setText(timeMin);
    return false;
}

static bool onButtonClick_ButtonMonSub(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonMonSub !!!\n");
    char timeMon[64];
    if(mon <= 1)
    {
    	mon = 12;
    }
    else
    {
    	mon--;
    }
    sprintf(timeMon,"%02d",mon);
    mTextViewMonPtr->setText(timeMon);

    if((year%4 == 0 && year%100 !=0) || (year%400 == 0))
    {
    	LOGD("闰年 29 \n");
    	modsetdat(daycTab[mon-1]);
	   LOGD("daycTab[mon-1]=%d\n",daycTab[mon-1]);
    }
    else
    {
    	LOGD("平年  28 \n");
    	modsetdat(dayTab[mon-1]);
    	LOGD("dayTab[mon-1]=%d\n",dayTab[mon-1]);
    }
    return false;
}

static bool onButtonClick_ButtonDaySub(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonDaySub !!!\n");
    char timeDay[64];
    if(day <= 1)
    {
    	day = daycTab[mon-1];
    	return false;
    }
    else
    {
    	day--;
    }
    sprintf(timeDay,"%02d",day);
    mTextViewMonnPtr->setText(timeDay);
    return false;
}

static bool onButtonClick_ButtonHourSub(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonHourSub !!!\n");
    char timeHour[64];
    if(hour <= 0)
    {
    	hour = 23;
    }
    else
    {
    	hour--;
    }
    sprintf(timeHour,"%02d",hour);
    mTextViewHourPtr->setText(timeHour);
    return false;
}

static bool onButtonClick_ButtonMinSub(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonMinSub !!!\n");
    char timeMin[64];
    if(minn <= 0)
    {
    	minn = 59;
    }
    else
    {
    	minn--;
    }
    sprintf(timeMin,"%02d",minn);
    mTextViewMinPtr->setText(timeMin);
    return false;
}

static bool onButtonClick_hide(ZKButton *pButton) {
    LOGD(" ButtonClick hide !!!\n");
    EASYUICONTEXT->closeActivity("timeActivity");
    return false;
}

static bool onButtonClick_ui_back(ZKButton *pButton) {
    LOGD(" ButtonClick ui_back !!!\n");
//    EASYUICONTEXT->closeActivity("timeActivity");
    EASYUICONTEXT->goBack();
    return false;
}
